<?php

namespace App\Exports;

use App\Models\OrderChild;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithStyles;
//单元格格式化,单元格处理文本、数字、日期、金额等格式
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
//自动适应单元格宽
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;




class TestExport implements FromCollection, WithColumnFormatting, ShouldAutoSize,WithStyles
{
    /**订单导出**/
    private $params;
    /**
     * @var string[] 导出标题集合
     */
    private $headingList = [
        ['工资详情','','','','','','','','','',''],
        ['','','','','','','','','','',''],
        [
            'employee_name'=>'员工姓名',
            'start_time'=>'开始时间',
            'end_time'=>'结束时间',
            'product_name'=>'产品名称',
            'color'=>'产品颜色',
            'customer_name'=>'客户名称',
            'model_number'=>'型号',
            'specification'=>'规格',
            'base_pay'=>'员工工资',
            'price'=>'工单金额',
            'total'=>'合计'
        ]
    ];

    private $title='员工工资详情';



    public function __construct($params) {
        ob_end_clean();
        $this->params = $params;
    }

    //数组转集合
    public function collection()
    {
        return new Collection($this->createData());
    }

    //业务代码
    public function createData()
    {
        $data[] = $this->headingList;
        $params = $this->params;
        $array = OrderChild::Search($params)
            ->with(['order'=>function($q){
                $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }])->select('id','color_id','product_id','customer_id','model_number_id','specification_id');
            },
                'employee'=>function($q){
                    $q->select('user_id','employee_name','base_pay');
                }
            ])
            ->select('id','order_id','price','employee_user_id')
            ->get()->toArray();
        foreach ($array as $key=>&$val){
            $data[] = [
                'employee_name'=>$val['employee']['employee_name'],
                'start_time'=>$params['start_time'],
                'end_time'=>$params['end_time'],
                'product_name'=>$val['order']['product']['product_name'],
                'color'=>$val['order']['color']['color'],
                'customer_name'=>$val['order']['customer']['customer_name'],
                'model_number'=>$val['order']['model_number']['model_number'],
                'specification'=>$val['order']['specification']['specification'],
                'base_pay'=>$val['employee']['base_pay'],
                'price'=>$val['price'],
                'total'=>$val['employee']['base_pay'] + $val['price']
            ];
        }
        return $data;
    }


    public function columnFormats(): array
    {
        //'B' => NumberFormat::FORMAT_DATE_DDMMYYYY, //日期
        $format = NumberFormat::FORMAT_NUMBER_00;//金额保留两位小数
        return ['I' => $format, 'J' => $format,'K' => $format];

    }


    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->title;
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 100, 'B' => 100, 'C' => 100, 'D' => 100, 'E' => 100, 'F' => 100,'G' => 100, 'H' => 100, 'I' => 100, 'J' => 100,'K' => 100
        ];
    }




    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
        $sheet->mergeCells('A1:K2'); //合并单元格
        $sheet->getStyle('A1:K2')
            ->applyFromArray([
                'font' => ['bold' => true],
                'color' => ['rgb' => '0072ff']
            ]);//字体设置
        // 设置背景色
        $sheet->getStyle('A1:K2')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setRGB('d1d2d2');

        // setWrapText自动换行，setVertical垂直对齐方式
        $sheet->getStyle("A:K")->getAlignment()->setVertical('center');
        $sheet->getStyle('A:K')->applyFromArray(['alignment' => ['horizontal' => 'center']]);//设置水平居中


    }


}
